#!/usr/bin/env bash

UBUNTU_SERVER_DEPLOY=1

HTTP_PROXY=

TIMEZONE=Asia/Shanghai

DNS=

SERVER_REQUIRE_PKGS="build-essential \
bison \
openssl \
libreadline6 libreadline6-dev \
curl \
git git-core \
zlib1g zlib1g-dev \
libssl-dev \
libyaml-dev libxml2-dev libxslt-dev \
autoconf libc6-dev \
libc6-dev \
g++ gcc \
unzip \
p7zip-full"

APT_IGN_PKGS="oracle-java8-installer"

TEMP_DIR=/tmp
PREFIX_HOME=/usr/local

#########################################################
# Tengine prepare
#########################################################
TENGINE_TEMP_DIR=${TEMP_DIR}/tengine

TENGINE_REQUIRE_PKGS="libjemalloc-dev libjemalloc1 libjemalloc1-dbg \
libssl-dev \
libxml2-dev libxslt1-dev \
libgd-dev \
libgeoip-dev \
sysv-rc-conf"

TENGINE_NAME=tengine-2.1.0
TENGINE_SOURCE=http://tengine.taobao.org/download/${TENGINE_NAME}.tar.gz

PCRE_NAME=pcre-8.36
PCRE_SOURCE=ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/${PCRE_NAME}.tar.gz

ZLIB_NAME=zlib-1.2.8
ZLIB_SOURCE=http://zlib.net/${ZLIB_NAME}.tar.gz

OPENSSL_NAME=openssl-1.0.2a
OPENSSL_SOURCE=https://www.openssl.org/source/${OPENSSL_NAME}.tar.gz

#########################################################
# Tengine setup
#########################################################
WWW_USER=www-data
WWW_GROUP=www-data

TENGINE_HOME=${PREFIX_HOME}/tengine

TENGINE_CONF_DIR=/etc/tengine
TENGINE_PID_FILE=/var/run/tengine.pid

TENGINE_LOG_DIR=/var/log/tengine
TENGINE_TMP_DIR=/var/tmp/tengine

TENGINE_TMP_CLIENT=${TENGINE_TMP_DIR}/client_body
TENGINE_TMP_PROXY=${TENGINE_TMP_DIR}/proxy
TENGINE_TMP_FCGI=${TENGINE_TMP_DIR}/fcgi
TENGINE_TMP_UWSGI=${TENGINE_TMP_DIR}/uwsgi
TENGINE_TMP_SCGI=${TENGINE_TMP_DIR}/scgi

#########################################################
# Tengine configure command
#########################################################
TENGINE_CONF_CMD="./configure \
--prefix=${TENGINE_HOME} \
--sbin-path=/usr/sbin/tengine \
--conf-path=${TENGINE_CONF_DIR}/tengine.conf \
--error-log-path=${TENGINE_LOG_DIR}/error.log \
--http-log-path=${TENGINE_LOG_DIR}/access.log \
--pid-path=${TENGINE_PID_FILE} \
--user=${WWW_USER} \
--group=${WWW_GROUP} \
--with-poll_module \
--with-file-aio \
--without-dso \
--with-http_ssl_module \
--with-http_spdy_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_slice_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_concat_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_sysguard_module \
--with-pcre=${TENGINE_TEMP_DIR}/${PCRE_NAME} \
--with-backtrace_module \
--with-jemalloc \
--with-zlib=${TENGINE_TEMP_DIR}/${ZLIB_NAME} \
--with-openssl=${TENGINE_TEMP_DIR}/${OPENSSL_NAME} \
--http-client-body-temp-path=${TENGINE_TMP_CLIENT} \
--http-proxy-temp-path=${TENGINE_TMP_PROXY} \
--http-fastcgi-temp-path=${TENGINE_TMP_FCGI} \
--http-uwsgi-temp-path=${TENGINE_TMP_UWSGI} \
--http-scgi-temp-path=${TENGINE_TMP_SCGI}"

#########################################################
# Tengine config define
#########################################################
TENGINE_WORK_PROS=8
TENGINE_RLIMIT=102400
TENGINE_WORKER_CONNS=102400
TENGINE_DEFAULT_MIME_TYPE=text/html
TENGINE_DEFAULT_CHARSET=text/html
TENGINE_VHOST_LOG_DIR=/var/log/vhost
TENGINE_VHOST_CONF_DIR=${TENGINE_CONF_DIR}/vhost

TENGINE_REQUIRE_DIRS[1]=${TENGINE_LOG_DIR}
TENGINE_REQUIRE_DIRS[2]=${TENGINE_VHOST_LOG_DIR}
TENGINE_REQUIRE_DIRS[3]=${TENGINE_TMP_DIR}
TENGINE_REQUIRE_DIRS[4]=${TENGINE_TMP_CLIENT}
TENGINE_REQUIRE_DIRS[5]=${TENGINE_TMP_PROXY}
TENGINE_REQUIRE_DIRS[6]=${TENGINE_TMP_FCGI}
TENGINE_REQUIRE_DIRS[7]=${TENGINE_TMP_UWSGI}
TENGINE_REQUIRE_DIRS[8]=${TENGINE_TMP_SCGI}
TENGINE_REQUIRE_DIRS[9]=${TENGINE_VHOST_CONF_DIR}

TENGINE_WWW_HOME=/var/www

#########################################################
# Memcached basic config
#########################################################
MC_HOST=127.0.0.1
MC_PORT=11211
MC_LOG_FILE=/var/log/memcached.log
MC_USER=memcache
MC_SIZE=64

#########################################################
# mcrouter
#########################################################
MCR_FOLLY_SOURCE=https://github.com/facebook/folly
MCR_FOLLY_REQ_PKGS="g++ \
automake \
autoconf \
autoconf-archive \
autoconf-archive \
libtool \
libboost-all-dev \
libevent-dev \
libdouble-conversion-dev \
libgoogle-glog-dev \
libgflags-dev \
liblz4-dev \
liblzma-dev \
libsnappy-dev \
make \
zlib1g-dev \
binutils-dev \
libjemalloc-dev \
libssl-dev \
libiberty-dev"

MCR_DOUBLE_CONV_SOURCE=https://github.com/floitsch/double-conversion
MCR_DOUBLE_CONV_INSTALL_DIR=${PREFIX_HOME}/double-conversion

MCR_SOURCE=https://github.com/facebook/mcrouter
MCR_REQ_PKGS="gcc-4.8 g++-4.8 libboost1.54-dev libboost-thread1.54-dev \
libboost-filesystem1.54-dev libboost-system1.54-dev libboost-regex1.54-dev \
libboost-python1.54-dev libboost-context1.54-dev ragel autoconf unzip \
git libtool python-dev cmake libssl-dev libcap-dev libevent-dev \
libgtest-dev libsnappy-dev scons binutils-dev make \
wget libdouble-conversion-dev libgflags-dev libgoogle-glog-dev"

MCR_PORT=5000
MCR_CONF_FILE=/etc/mcrouter.json
MCR_LOG_FILE=/var/log/mcrouter.log
MCR_PID_FILE=/var/run/mcrouter.pid
MCR_POOL_SERVERS=

#########################################################
# Golang install
#########################################################
GO_TEMP_DIR=/tmp/golang

GO_VER=1.4.2
GO_OS=linux
GO_ARCH=amd64
GO_PKG_FILE=go${GO_VER}.${GO_OS}-${GO_ARCH}.tar.gz
GO_SOURCE=https://storage.googleapis.com/golang/${GO_PKG_FILE}

GO_INSTALL_DIR=/usr/local

#########################################################
# PHP install
#########################################################
# PHP temp dir
PHP_TEMP_DIR=

# Used version
PHP_VER=5.5.23
PHP_PKG_NAME=php-${PHP_VER}
PHP_PKG_FILE=${PHP_PKG_NAME}.tar.gz

# download source
# china
PHP_CN_SOURCE=http://cn2.php.net/distributions/${PHP_PKG_FILE}
# hongkong
PHP_HK_SOURCE=http://php.net/distributions/${PHP_PKG_FILE}
# USA
PHP_US_SOURCE=http://php.net/distributions/${PHP_PKG_FILE}
PHP_SOURCE=${PHP_HK_SOURCE}

PHP_INSTALL_DIR=${PHP_PKG_NAME}
PHP_HOME=${PREFIX_HOME}/${PHP_INSTALL_DIR}

PHP_BIN_PREFIX=php

PHP_REQ_PKGS=

PHP_CONF_CMD="./configure \
--prefix=${PHP_HOME} \
--enable-fpm \
--with-fpm-user=${WWW_USER} \
--with-fpm-group=${WWW_GROUP} \
--with-openssl \
--with-pcre-regex \
--with-zlib \
--enable-bcmath \
--with-bz2 \
--with-gd \
--with-mcrypt \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--enable-gd-native-ttf \
--with-mhash \
--with-curl \
--enable-intl \
--enable-mbstring \
--with-mysql \
--with-mysql-sock \
--with-mysqli \
--with-pdo-mysql \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-zip \
--enable-calendar \
--enable-mysqlnd \
--with-pear \
--enable-opcache"

#########################################################
# PHP-FPM
#########################################################
PHP_FPM_LOG_DIR=/var/log/${PHP_BIN_PREFIX}-fpm
PHP_FPM_PID_FILE=/var/run/${PHP_BIN_PREFIX}-fpm.pid
PHP_FPM_ERR_FILE=${PHP_FPM_LOG_DIR}/error.log
PHP_FPM_LOG_LEVEL=warning

PHP_FPM_PROCESS_MAX=10
PHP_FPM_PROCESS_TIMEOUT=30
PHP_FPM_RLIMIT=65535

PHP_FPM_LISTEN="127.0.0.1:9902"
PHP_FPM_LISTEN_MODE=0666
PHP_FPM_PM_MODE=static
PHP_FPM_PM_START=10
PHP_FPM_PM_MAX_CHILDREN=10
PHP_FPM_PM_MAX_REQUESTS=0

#########################################################
# php.ini
#########################################################
PHP_INI_MAX_EXECUTION_TIME=60
PHP_INI_MAX_INPUT_TIME=60
# please change error_reporting by yourself
#PHP_INI_ERR_REPORTING=
PHP_INI_DEFAULT_CHARSET=UTF-8
PHP_INI_POST_MAX_SIZE=10M
PHP_INI_UPLOAD_MAX_SIZE=10M